angular.module('Controller')

.controller('QgyPromotionManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            ruleName:undefined
            status:undefined
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.priceStatusList=commonConstant.priceStatusList(true)
    $scope.appointmentStatusList=commonConstant.appointmentStatusList(true)
    $scope.settleWayList=commonConstant.promotionSettleWayList(true)
    $scope.channelList=commonConstant.priceChannelList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.leaseBinList=commonConstant.leaseBinList(true)
    $scope.cityList=[{value:0,name:'全部'}].concat(commonConstant.qgyCityList())
    loadApartment=()->
        $scope.apartmentList=[{id:0,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )


    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            ruleType:2,
            status:$scope.search.status
            ruleName:$scope.search.ruleName
        }
        $http.get('/housePrice',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={ruleType:2}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/goods/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('定价策略删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )

    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyPriceManageModalCtrl',
            templateUrl: 'templates/price-manage/price-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)